/**
 * Copyright 2014,Peak Tai 台俊峰(taijunfeng_it@sina.com).
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * 		http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.cenluan.gallery.member;

import java.util.Date;

import com.cenluan.common.Const;
import com.cenluan.gallery.Gallery;
import com.cenluan.kit.UUIDKit;
import com.cenluan.me.call.Call;
import com.cenluan.notification.Notification;
import com.cenluan.user.User;
import com.jfinal.core.Controller;
import com.jfinal.kit.StrKit;

/**
 * 相册成员
 * 
 * @author peak
 * 
 */
public class GalleryMemberController extends Controller {

	/**
	 * 加入，显示加入页面
	 */
	public void add() {
		Gallery gallery = Gallery.dao.findById(getPara());
		if (!Gallery.STATE_ACTIVE.equals(gallery.getStr("state"))) {
			renderError(403);
			return;
		}
		// 如果相册没有密码，禁止操作
		if (StrKit.isBlank(gallery.getStr("pwd"))) {
			renderError(403);
			return;
		}
		setAttr("gallery", gallery);
	}

	/**
	 * 保存，核对密码，正确就保存数据
	 */
	public void save() {
		Gallery gallery = Gallery.dao.findById(getPara());
		String pwd = getPara("pwd");
		// 如果相册没有密码，禁止操作
		if (StrKit.isBlank(gallery.getStr("pwd"))) {
			setAttr("msg", "该相册禁止加入");
			renderJson();
			return;
		}
		if (!pwd.equals(gallery.getStr("pwd"))) {
			setAttr("msg", "密码不正确");
			renderJson();
			return;
		}
		User user = getSessionAttr(Const.SESSION_USER);
		if (gallery.getStr("createrId").equals(user.getStr("id"))) {
			setAttr("ok", true);
			renderJson();
			return;
		}
		GalleryMember member = new GalleryMember();
		member.set("id", UUIDKit.generateUUID());
		member.set("galleryId", getPara());
		member.set("userId", user.getStr("id"));
		member.set("active", true);
		member.set("joinTime", new Date());
		member.save();
		// 通知
		Call.save(gallery.getStr("createrId"), Call.TYPE_JOIN_GALLERY,
				gallery.getStr("id"));
		setAttr("ok", true);
		renderJson();

	}

	/**
	 * 展示
	 */
	public void show() {
		User user = getSessionAttr(Const.SESSION_USER);
		GalleryMember member = GalleryMember.dao.findById(getPara());
		Gallery gallery = member.getGallery();
		setAttr("galleryCreater",
				gallery.getStr("createrId").equals(user.getStr("id")));
		setAttr("member", member);
	}

	/**
	 * 封禁
	 */
	public void disable() {
		User user = getSessionAttr(Const.SESSION_USER);
		GalleryMember member = GalleryMember.dao.findById(getPara());
		Gallery gallery = member.getGallery();
		if (!user.isAdmin()
				&& !user.getStr("id").equals(gallery.getStr("createrId"))) {
			renderError(403);
			return;
		}

		member.set("active", false).keep("id", "active").update();
		setAttr("ok", true);
		renderJson();
		// 被封通知
		Notification.save(getPara(),
				user.getHtml() + "将您在相册" + gallery.getHtml()
						+ "中封禁，您将不能再为相册上传照片");

	}

	/**
	 * 启用
	 */
	public void enable() {
		User user = getSessionAttr(Const.SESSION_USER);
		GalleryMember member = GalleryMember.dao.findById(getPara());
		Gallery gallery = member.getGallery();
		if (!user.isAdmin()
				&& !user.getStr("id").equals(gallery.getStr("createrId"))) {
			renderError(403);
			return;
		}
		member.set("active", true).keep("id", "active").update();
		setAttr("ok", true);
		renderJson();
		// 解封通知
		Notification
				.save(getPara(), user.getHtml() + "将您在相册" + gallery.getHtml()
						+ "中解封，您可以继续上传照片了");
	}
}
